package com.sacc.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.power.common.model.CommonResult;
import com.sacc.mapper.BoxInfoMapper;
import com.sacc.pojo.BoxInfo;
import com.sacc.service.BoxInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 滚~韬
 * @date 2022/4/12 13:29
 */
@Service
public class BoxInfoServiceImpl implements BoxInfoService {
    @Autowired
    private BoxInfoMapper boxInfoMapper;

    /**
     * 根据蜂箱id去获取boxinfo
     * @param boxId
     * @return
     */
    public BoxInfo Get(Integer boxId){
        return boxInfoMapper.selectById(boxId);
    }

    /**
     * 根据蜂箱id去获取boxinfo的内容
     * @param boxId
     * @return
     */
    public BoxInfo GetContent(Integer boxId){
        return boxInfoMapper.selectOne(new QueryWrapper<BoxInfo>()
                .select("box_content")
                .eq("box_id",boxId));
    }
    /**
     * 添加一个boxInfo
     * @param boxInfo
     * @return
     */
    public CommonResult Add(BoxInfo boxInfo){
        try{
            if(boxInfoMapper.selectById(boxInfo.getBoxId())!=null){
                boxInfoMapper.updateById(boxInfo);
                return new CommonResult("200","更新成功");
            }
            boxInfo.setCreateTime(LocalDateTime.now());
            boxInfoMapper.insert(boxInfo);
            return new CommonResult("200","添加成功");
        }catch (Exception e){
            return new CommonResult("400","添加失败");
        }
    }
}
